import {mapGetters, mapMutations} from "vuex";
import {EventBus, FunEventType} from "@/event";
import {getFunTabMetas} from "@/metas/FunTabMetas";
import {ComponentType} from "@/store/consts";
import {EditEventType} from "@/metas/EditTabMetas";
import {FuncMetasMap} from "@/components/OptionsMetaData";

export const MixinEditConfig = {
    props: ['global', 'view'],
    data() {
        return {
            modalMetas: [],
            viewConfig: {},
            modalConfigs: [],
            type: ComponentType.Edit
        }
    },
    computed: {
        ...mapGetters({
            meta: 'editMeta',
            metas: 'editMetas',
            model: 'editTabModel',
            visible: 'editVisible',
            funcMetas: 'editFunMetas'
        })
    },
    created() {
        // 防止多次监听
        if(!EventBus.allEvents().has(FunEventType.changeEditFunc)) {
            EventBus.listener(FunEventType.changeEditFunc, (value) => {
                let map = value.map(func => { return {func, name: FuncMetasMap[func]}});
                this.funcMetas.length = 0;
                this.funcMetas.push(...map)
            })
        }
    },
    mounted() {
        EventBus.publish(EditEventType.Mount, this.meta);
        EventBus.publish(FunEventType.editFun, getFunTabMetas(ComponentType.Edit));
    },
    methods: {
        ...mapMutations(['initEditMeta', 'switchActiveMetaTo', 'removeEditMetas', 'switchEditVisible']),
        activeHandle() {
          // if(!this.meta.active) {
              this.switchActiveMetaTo(this.meta);
          // }
        },
        onAddHandle(e) {
            if(this.metas) {
                this.metas.forEach((meta, index) => {
                    if(meta.type == 'modal') {
                        this.modalMetas.push(meta);
                        this.metas.splice(index, 1);
                        this.global.modalMetas = this.modalMetas;
                    }
                });
            }
        },
        delMetaItem(meta) {
            this.removeEditMetas(meta);
            this.activeHandle();
        },
    }
}
